package com.cwt_taxi_xa.record;

import com.cwt_taxi_xa.model.RecorderInfo;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.protocol.Encoder;

import java.util.ArrayList;

/**
 * 录音列表上传管理者
 * @author wlh
 *
 */
public class RecorderListUploadManager {

	public static final String TAG = "RecorderListUploadManager";
	private static ArrayList<RecorderInfo> infos = null;	// 总数据
	private static ArrayList<RecorderInfo> subInfos;	// 当前分包
	private static int pkgSize = 0;	// 总包数
	private static int pkgCode = 0;	// 包序号
	private static int index = 0;
	private static int seq = 0;	// 流水号
	private static boolean isSubPkg = false;

	public static void upLoad(ArrayList<RecorderInfo> list, int seq){
		if(list != null && list.size() != 0){
			// 初始化
			infos = list;
			RecorderListUploadManager.seq = seq;
			// 一条音频数据的检索项是22字节， 一包就是 22 * 23 = 506
			if(list.size() <= 23){
				isSubPkg = false;
				DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(seq, 1, 0, infos, infos.size()));
			}else{
				isSubPkg = true;
				subInfos = new ArrayList<RecorderInfo>();
				index = 0;
				pkgCode = 0;
				if(list.size() % 23 == 0){
					pkgSize = list.size()/23;
				}else{
					pkgSize = list.size()/23 + 1;
				}

				for(int i = 0;i <list.size();i++){
					index++;
					subInfos.add(list.get(i));
					if(subInfos.size() == 23){
						DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(
								seq, pkgSize, pkgCode, subInfos, infos.size()));
						return;
					}
				}
			}
		}else{
			isSubPkg = false;
			DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(seq, 0, 0, null, 0));
		}
	}

	public static void progress() {
		// TODO Auto-generated method stub
		if(!isSubPkg){
			// 没有分包，收到应答就结束
			return;
		}
		if(pkgCode == pkgSize-1){
			return;
		}
		pkgCode ++;
		subInfos.clear();
		for(int i = index;i <infos.size();i++){
			index++;
			subInfos.add(infos.get(i));
			if(subInfos.size() == 23){
				DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(
						seq, pkgSize, pkgCode, subInfos, infos.size()));
				return;
			}
		}
		DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(
				seq, pkgSize, pkgCode, subInfos, infos.size()));
	}

	public static void reProgress() {
		// TODO Auto-generated method stub
		if(isSubPkg){
			--pkgCode;
			index = index - 23;
			progress();
		}else{
			if(infos != null && infos.size() != 0){
				DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(seq, 1, 0, infos, infos.size()));
			}else{
				DispatchCenterUsers.witeMsg(Encoder.multiRecordRetrievalResponse(seq, 0, 0, null, 0));
			}
		}
	}
}
